(function (window, angular, $) {
    var e = /[?&]([^=&?]+)=([^=&?]*)/g, r = null;
    window.params = {};
    while (r = e.exec(location.search))
        window.params[r[1]] = r[2];

    var myApp = angular.module('myApp');

    var loginPostUrl = null;
    myApp.config(function(mtAjaxProvider) {
        mtAjaxProvider.setUnauthorizedHandler(function(response) {
            loginPostUrl = response.headers('Location');
        });
    });

    myApp.run(function($rootScope, mtAjax) {
        $rootScope.pub = {};
        mtAjax("/scheduling/api/pub/userunion").then(function(data) {
            location.replace('/scheduling/');
        }, function(response){
        });

    });

    var code ;
    function createCode()
    {
        code = "";
        var codeLength = 4;//验证码的长度
        //所有候选组成验证码的字符，可以用中文
        /* var selectChar = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');  */
        var selectChar = new Array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
        for(var i=0;i<codeLength;i++)
        {
            var charIndex = Math.floor(Math.random()*selectChar.length);
            code +=selectChar[charIndex];
        }
        return code;
    }



    myApp.controller("loginController", function ($scope, $http, $rootScope, mtAjax) {

        $scope.user = {
                vcodesrc : ""
        };
        if(location.href.substring(0,location.href.indexOf(".")) == "http://tjdxjyxy"){
            $scope.user.site_id = 'TJDXJY';
        }else{
            $scope.user.site_id = 'PUB';
        }
        $scope.user.vcodesrc = "/scheduling/veriCode?code="+createCode();

        $scope.change = function(){
            $scope.user.vcodesrc = "/scheduling/veriCode?code="+createCode();

        };

        $scope.getKey = function($event){
            if(event.keyCode==13){
                $scope.ok();
            }

        };

    	$scope.modalShown = function (params) {
    		var a = params[2].relatedTarget;
    		$scope.user = {
    			site_id : $(a).attr('site-id')
    		};

    		$scope.site = $(a).attr('site-name');
    	};


        //重置
        $scope.reset = function() {
            $scope.user.username = "";
            $scope.user.password = "";
            $scope.user.vcode = "";
        };


        $scope.ok = function () {
            $scope.user.login_type='phone';
            if($scope.user.vcode != code){
                $('#loginfailed').modal();
                $scope.user.vcodesrc = "/scheduling/veriCode?code="+createCode();
                return;
            }
            var params={
                userName:$scope.user.username
            };

            mtAjax("/scheduling/api/anon/pub/getusertype", params).then(function(data) {
                $scope.user.login_type=data[0][0].login_type;
                $http.post(loginPostUrl, $.param($scope.user), {
                    headers: {
                        "X-Requested-With": "XMLHttpRequest",
                        "Content-Type": "application/x-www-form-urlencoded"
                    }
                }).then(function (response) {
                    location.replace('/scheduling/html/pub/homeframe');
                }, function (response) {
                    $('#loginfailed').modal();
                    $scope.user.vcodesrc = "/scheduling/veriCode?code="+createCode();
                    $scope.user.vcode = "";
                });

            }, function(response){
                $('#loginfailed').modal();
                $scope.user.vcodesrc = "/scheduling/veriCode?code="+createCode();
                $scope.user.vcode = "";
            });




        };

        $scope.forgetPassword = function () {
            alert("请联系系统管理员！");
        };
    });

})(window, angular, jQuery, undefined);
